<?php

require_once("lib/JSON.php");
require_once 'lib/WareList.php';
require_once 'lib/WareForm.php';
require_once 'lib/CheckBox.php';
require_once 'lib/TextField.php';
require_once 'lib/WareListItem.php';
require_once 'lib/RadioButton.php';
require_once 'lib/RadioButtonItem.php';
require_once 'lib/Label.php';
require_once 'lib/Button.php';
require_once 'lib/Message.php';
require_once 'lib/WarePaymentForm.php';

//$json_code = $_GET['json_code'];
function jsonToWare($json_code) {
    $json = new Services_JSON();
//    echo $json_code;
    $dec = $json->decode($json_code);
//    var_dump($dec);
    if (isset($dec->list)) {
        $Wlist = new WareList($dec->list->title, $dec->list->list_id);
//        var_dump($dec->list->items);
        foreach ($dec->list->items as $item) {
            if (isset($item)) {// This will allow the list to end with a comma which mean json creates a null object
                $WLItem = new WareListItem($item->id, $item->label);
                $Wlist->addItem($WLItem);
            }
        }
        if (isset($dec->list->message)) {
            $WLItem = new Message($dec->list->message->text);
            $Wlist->addMessage($WLItem);
        }
        return $Wlist->getCode();
//        return "something";
    } else if (isset($dec->form)) {
        $finish = (isset($dec->form->finish)) ? $dec->form->finish : "true";
        $Wform = new WareForm($dec->form->title, $dec->form->form_id, $finish);
        foreach ($dec->form->items as $item) {
            if (isset($item->textfield)) {
                $WFItem = new TextField($item->textfield->id, $item->textfield->label);
                $Wform->addItem($WFItem);
            } else if (isset($item->checkbox)) {
                $WFItem = new CheckBox($item->checkbox->id, $item->checkbox->label);
                $Wform->addItem($WFItem);
            } else if (isset($item->label)) {
                $WFItem = new Label($item->label->text);
                $Wform->addItem($WFItem);
            } else if (isset($item->button)) {
                $WFItem = new Button($item->button->label);
                $Wform->addItem($WFItem);
            } else if (isset($item->message)) {
                $WFItem = new Message($item->message->text);
                $Wform->addItem($WFItem);
            } else if (isset($item->radiobutton)) {
                $WFItem = new RadioButton($item->radiobutton->id, $item->radiobutton->label);
                foreach ($item->radiobutton->choices as $choice) {
                    $RBItem = new RadioButtonItem($choice->choice);
                    $WFItem->addItem($RBItem);
                }
                $Wform->addItem($WFItem);
            } else {//commment out to allow commas at the end of a list of may be check boxes, but at the risk of more difficult debugging
//                return "JSON API Sysntax Error (Unrecognized property in form: )";
            }
        }
        return $Wform->getCode();
    } else if (isset($dec->pay)) {
//        {pay:{
//            amount:50}}
        $Wform = new WarePaymentForm($dec->pay->amount);
//        var_dump($dec);
        return $Wform->getCode();
    } else if (isset($dec->courses)) {
        return $dec->courses;
    } else if (isset($dec->lessons)) {
        return $dec->lessons;
    } else if (isset($dec->pages)) {
        return $dec->pages;
    } else {
        var_dump($dec);
        return "JSON API Sysntax Error (Neither list nor form nor payment)";
    }
}
